package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.GuestLogic;
import com.hotel.BL.SalutationLogic;

import data.Guest;
import data.Salutation;
import data.passingData.GuestData;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class GuestService extends HttpServlet{
	//http://localhost:8080/guestService?func=addGuest&title=1
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("delGuest")){
				DeleteGuest(req,resp);
			}else if(doClass.equals("changeStatus")){
				ChangeStatus(req,resp);
			}else if(doClass.equals("GetAllGuest4Admin")){
				GetAllGuest4Admin(req,resp);
			}else if(doClass.equals("getGuestById")){
				GetGuestByIdForEditDialog(req,resp);
			}else if(doClass.equals("getGuestInFoById")){
				GetGuestByIdForGuestInfoDialog(req,resp);
			}else if(doClass.equals("editGuestData")){
				EditGuestData(req,resp);
			}else if(doClass.equals("delGuest")){
				DeleteGuest(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void DeleteGuest(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		
		GuestLogic guestLogic = new GuestLogic(req,resp);
		for(int i = 0; i < id.split(",").length ; i++){
			guestLogic.DeleteGuest(hotelId,id.split(",")[i]);
		}

		GetAllGuest4Admin(req, resp);
	}
	
	private void ChangeStatus(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");

		Object obj = new GuestLogic(req,resp).ChangeStatus(hotelId,id);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetAllGuest4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		GuestLogic guestLogic = new GuestLogic(req,resp);
		List<HashMap<String, Object>> obj = guestLogic.GetAllGuest4AdminLimit(hotelId,offset,line);
		double dataSize = guestLogic.GetAllGuest4Admin(hotelId).size();
		
		Gson gson = new Gson();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetGuestByIdForEditDialog(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long id = Long.parseLong(req.getParameter("id"));
		Guest guest = new GuestLogic(req,resp).GetGuest(id);
		List<Salutation> sal = new SalutationLogic().GetAllSalutation(hotelId);
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("sal", sal);
		data.put("data", guest);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetGuestByIdForGuestInfoDialog(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		GuestLogic guestLogic = new GuestLogic(req,resp);
		Guest guest = guestLogic.GetGuest(id);
		GuestData guestdata = guestLogic.SetToGuestData(guest);
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(guestdata));
	}
	private void EditGuestData(HttpServletRequest req, HttpServletResponse resp)
			throws ParseException, IOException, EntityNotFoundException, JSONException, ServletException {
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		long id = Long.parseLong(req.getParameter("id"));
		 String Name =req.getParameter("Name");
		 String lastName =req.getParameter("lastName");
		 String SName =req.getParameter("SName");
		 String mail=req.getParameter("mail");
		 String gender=req.getParameter("gender");
		 String birthday=req.getParameter("birthday");
		 String nationalityString=req.getParameter("nationalityString");
		 String phone=req.getParameter("phone");
		 String idNumber=req.getParameter("idNumber");
		 String passportNumber=req.getParameter("passportNumber");
		 String driverlicense=req.getParameter("driverlicense");
		 String socialSecuritycard=req.getParameter("socialSecuritycard");
		 String addressLine=req.getParameter("addressLine");
		 String country=req.getParameter("country");
		 String state=req.getParameter("state");
		 String city=req.getParameter("city");
		 String pincode=req.getParameter("pincode");
		 String fax=req.getParameter("fax");
		 String company=req.getParameter("company");
		 String officeAddress=req.getParameter("officeAddress");
		 String officeCountry=req.getParameter("officeCountry");
		 String officeState=req.getParameter("officeState");
		 String officeCity=req.getParameter("officeCity");
		 String officePincode=req.getParameter("officePincode");
		 String officePhone=req.getParameter("officePhone");
		 String officeFax=req.getParameter("officeFax");
		 String officeEmail=req.getParameter("officeEmail");
		 String	designation=req.getParameter("designation"); 
		 boolean isVIP=Boolean.parseBoolean(req.getParameter("isVIP"));
		 boolean isBlackList=Boolean.parseBoolean(req.getParameter("isBlackList"));
		 String note=req.getParameter("note");
		new GuestLogic(req,resp).EditGuestData(hotelId,Name,lastName,SName,mail,gender,birthday,nationalityString
				,phone,idNumber,passportNumber,driverlicense,socialSecuritycard
				,addressLine,country,state,city,pincode,fax,company
				,officeAddress,officeCountry,officeState,officeCity,officePincode,officePhone
				,officeFax,officeEmail,isVIP,isBlackList,note,id,designation);

		GetAllGuest4Admin(req, resp);
	}
}
